package com.yz.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yz.entity.Product;
import com.yz.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductService {
    @Autowired
    ProductMapper productMapper;
    public Page<Product> getAllProducts(int pageIndex){
        Page<Product> page = PageHelper.startPage(pageIndex,2);
//        Example example = new Example(Product.class);
//        example.setOrderByClause("product_id desc");
//        productMapper.selectByExample(example);
        productMapper.selectAllProducts();
        return page;
    }

    public int addProduct(Product product){
        int result = productMapper.insert(product);
        return result;
    }

    public int removeProductById(int productId){
        int result = productMapper.deleteByPrimaryKey(productId);
        return result;
    }

    public Product getProductById(int productId){
        Product product = productMapper.selectByPrimaryKey(productId);
        return product;
    }

    public int EditProduct(Product product){
        int result = productMapper.updateByPrimaryKeySelective(product);
        return result;
    }
}
